package com.cn.kabalifemerchants.ui.login;

import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.ui.login.bean.CooperationSreviceListBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;

/**
 * Created by 晓林 on 2020/9/9.
 * function:
 */
public class CooperationSrevicePresenter extends BasePresenter<CooperationSreviceView> {

    /**
     * 上传文件。
     */
    public void onUpImgData(String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", "head");
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }

    public void onCooperationSreviceListData() {
        OkGo.<CooperationSreviceListBean>get(Api.API_INDEX_SREVICE_TYPE_LIST)
                .params("type", 1)
                .execute(new CacheCallBack<CooperationSreviceListBean>() {
                    @Override
                    public void onSuccess(Response<CooperationSreviceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCooperationSreviceListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
